﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using ActiveUp.Net.Samples.Utils;
using ActiveUp.Net.Mail;
using System.Threading;

namespace OutlookInvitationTest1
{
    public partial class ImapNotifyConnection : System.Web.UI.Page
    {


        public static Imap4Client imap;
        public BackgroundWorker worker;
        


        protected void Page_Load(object sender, EventArgs e)
        {
            worker = new BackgroundWorker();

        }





        protected void Button1_Click(object sender, System.EventArgs e)
        {
            
            
            worker.DoWork += new DoWorkEventHandler(StartIdleProcess);

        }



        protected void Button3_Click(object sender, System.EventArgs e)
        {
            if (worker.IsBusy)
                worker.CancelAsync();

            worker.RunWorkerAsync();

            TextBox1.Text += "Notification Received Started. Waiting for a new message...\n";
        }
        

        private void StartIdleProcess(object sender, DoWorkEventArgs e)
        {
            
            // Get the BackgroundWorker that raised this event.
            //BackgroundWorker worker = sender as BackgroundWorker;

            if (imap != null && imap.IsConnected)
            {
                imap.StopIdle();
                imap.Disconnect();
            }

            imap = new Imap4Client();
            imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived);
            //worker.ReportProgress(1, "Connection...");
            imap.ConnectSsl("imap.gmail.com", 993);
            //worker.ReportProgress(0, "Login...");
            imap.Login(@"kontakttestowy@gmail.com", "kontaktestowy");
            //worker.ReportProgress(0, "Select 'inbox'...");
            imap.SelectMailbox("inbox");
            //worker.ReportProgress(0, "Start idle...");
            imap.StartIdle();
        }

        public  void NewMessageReceived(object source, NewMessageReceivedEventArgs e)
        {
            
            TextBox1.Text += "New message received :" + e.MessageCount+"\n";


            //imap.StopIdle();
        }

        protected void Button4_Click(object sender, System.EventArgs e)
        {

        }

  


        
        
        //protected void Button1_Click(object sender, EventArgs e)
        //{

        //    imap.ConnectSsl("imap.gmail.com", 993);

        //    imap.Login(@"kontakttestowy@gmail.com", "kontaktestowy");

        //    Mailbox inbox = imap.SelectMailbox("INBOX");

        //    imap.NewMessageReceived += new NewMessageReceivedEventHandler(imap_NewMessageReceived);
        //    imap.NewMessageReceived += new 

        //    inbox.Subscribe();

        //    imap.StartIdle();

        //    //imapClient.ConnectSsl("imap.gmail.com", 993);
        //    //imapClient.Login(@"kontakttestowy@gmail.com", "kontaktestowy");
            

        //    //Mailbox mails = imapClient.SelectMailbox("Inbox");
        //    //MessageCollection messages = mails.SearchParse("UNSEEN");
        //    //imapClient.NewMessageReceived += new NewMessageReceivedEventHandler(imap_NewMessageReceived);            
            
        //    //imapClient.StartIdle();

        //    //while (true) {
        //    //    Thread.Sleep(200);
        //    //    LabelStatus.Text = "message count" + mails.MessageCount;
        //    //}


        //    //IEnumerable<Message> unseen = messages.Cast<Message>();

        //    //foreach (Message email in unseen)
        //    //{
        //    //    TextBox1.Text += string.Format("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text);
        //    //    TextBox1.Text += "\n";

        //    //    if (email.Attachments.Count > 0)
        //    //    {
        //    //        foreach (MimePart attachment in email.Attachments)
        //    //        {
        //    //            TextBox1.Text += string.Format("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType);
        //    //        }
        //    //    }
        //    //    TextBox1.Text += "\n";
        //    //    TextBox1.Text += "\n";
        //    //}
        //    inbox.Subscribe();
        //}

        //public void imap_NewMessageReceived(object sender, NewMessageReceivedEventArgs e)
        //{
        //    LabelStatus.Text = string.Format("{0} new message(s) were received!", e.MessageCount);
        //}




        ////    public class MailRepository
        ////    {
        ////        private Imap4Client _client = null;

        ////        public MailRepository(string mailServer, int port, bool ssl, string login, string password)
        ////        {
        ////            if (ssl)
        ////                Client.ConnectSsl(mailServer, port);
        ////            else
        ////                Client.Connect(mailServer, port);
        ////            Client.Login(login, password);
        ////        }

        ////        public IEnumerable<Message> GetAllMails(string mailBox)
        ////        {
        ////            return GetMails(mailBox, "ALL").Cast<Message>();
        ////        }

        ////        public IEnumerable<Message> GetUnreadMails(string mailBox)
        ////        {
        ////            return GetMails(mailBox, "UNSEEN").Cast<Message>();
        ////        }

        ////        protected Imap4Client Client
        ////        {
        ////            get
        ////            {
        ////                if (_client == null)
        ////                    _client = new Imap4Client();
        ////                return _client;
        ////            }
        ////        }

        ////        private MessageCollection GetMails(string mailBox, string searchPhrase)
        ////        {
        ////            Mailbox mails = Client.SelectMailbox(mailBox);
        ////            MessageCollection messages = mails.SearchParse(searchPhrase);
        ////            return messages;
        ////        }
        ////    }
        ////}


    }
}